package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.ProjectPlansDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.selectByPage(name, status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return nursingPlanVoPageResponse;
    }

    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.insertNursingPlan(bean);
        Long planId = bean.getId();
        List<ProjectPlansDto> projectPlansDtos = nursingPlanDto.getProjectPlans();
        List<ProjectPlans> projectPlans = BeanUtil.copyToList(projectPlansDtos, ProjectPlans.class);
        if (!CollectionUtils.isEmpty(projectPlans)) {
            for (ProjectPlans projectPlan : projectPlans) {
                projectPlan.setPlanId(planId);
            }
            nursingPlanMapper.insertProjectPlan(projectPlans);
        }
    }

    @Override
    public NursingPlanVo selectById(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);


        return nursingPlanVo;
    }

    @Transactional
    @Override
    public void update(Long id, NursingPlanDto nursingPlanDto) {
        NursingPlan bean = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        bean.setId(id);
        nursingPlanMapper.updateNursingPlan(bean);
        //删除关联表所有数据
        nursingPlanMapper.deleteAllProPlan(id);
        //插入
        List<ProjectPlansDto> projectPlansDtos = nursingPlanDto.getProjectPlans();
        List<ProjectPlans> projectPlans = BeanUtil.copyToList(projectPlansDtos, ProjectPlans.class);
        if (!CollectionUtils.isEmpty(projectPlans)) {
            for (ProjectPlans projectPlan : projectPlans) {
                projectPlan.setPlanId(id);
            }
            System.out.println(projectPlans);
            nursingPlanMapper.insertProjectPlan(projectPlans);
        }
    }

    @Override
    public void able(Long id, Integer status) {
        nursingPlanMapper.able(id, status);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        nursingPlanMapper.deleteNursingPlan(id);

        nursingPlanMapper.deleteAllProPlan(id);
    }

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectNursingPlans();
        List<NursingPlanVo> nursingProjectVos = BeanUtil.copyToList(nursingPlans, NursingPlanVo.class);
        return nursingProjectVos;
    }
}
